<?php
namespace common\models\im;

use common\models\Common;
use common\models\Status;
use common\services\upload\AliyunOSS;
use common\utils\ImageUtil;
use Yii;
use yii\base\Exception;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class ImQrcode extends Common
{

    private $PLATFORM_DOWNLOAD_URL;
    public function __construct(array $config = [])
    {
        parent::__construct($config);
        $this->PLATFORM_DOWNLOAD_URL = Yii::$app->params['PLATFORM_DOWNLOAD_URL'];
    }

    // 生成用户二维码
    public function userQrCode($userId = 0, $txCode = '', $face = '') {
        $imageUtilModel = new ImageUtil();
        $aliyunOSSModel = new AliyunOSS();

        // 生成二维码
        $qrCode['user_id'] = $userId;
        $qrCode['user_tx_code'] = $txCode;
        $qrCode['type'] = Status::QR_CODE_TYPE_USER; // 用户二维码
        $jsonContent = json_encode($qrCode,true);
        $content = $this->PLATFORM_DOWNLOAD_URL."?".$jsonContent;
        $qrCodePathContent = $imageUtilModel->createLink($content,600);
        $qrCodePathPath = $aliyunOSSModel -> put('qrcode',$qrCodePathContent);

        // 合并二维码和图片
        $imageFace = static::getImages($face);
        $imageQrCode = static::getImages($qrCodePathPath);
        $qrCodeAndFacePathContent = $imageUtilModel->mergeFaceAndQrCode($imageQrCode,$imageFace,10,10,10,10);
        $qrCodeAndFacePathPath = $aliyunOSSModel -> put('qrcodeface',$qrCodeAndFacePathContent);
        return $qrCodeAndFacePathPath ?: '';
    }

    // 生成群头像
    public function groupPic($imgList = []) {
        $imageUtilModel = new ImageUtil();
        $aliyunOSSModel = new AliyunOSS();

        // 生成群头像
        $groupFaceContent = $imageUtilModel->getGroupAvatar($imgList);
        // 合并二维码和图片
        $groupFacePath = $aliyunOSSModel -> put('grouppic',$groupFaceContent);
        return $groupFacePath ?: '';
    }

    // 生成群二维码
    public function groupQrCode($groupId = 0, $txCode = '', $logo = 'default/avatar.png') {
//        set_time_limit(0);
        $imageUtilModel = new ImageUtil();
        $aliyunOSSModel = new AliyunOSS();

        // 生成二维码
        $qrCode['group_id'] = $groupId;
        $qrCode['group_tx_code'] = $txCode;
        $qrCode['type'] = Status::QR_CODE_TYPE_GROUP; // 群二维码
        $jsonContent = json_encode($qrCode,true);
        $content = $this->PLATFORM_DOWNLOAD_URL."?".$jsonContent;
        $qrCodePathContent = $imageUtilModel->createLink($content,220);
        $qrCodePathPath = $aliyunOSSModel -> put('qrgroupcode',$qrCodePathContent);

        // 合并二维码和图片
        $logo = static::getImages($logo);
        $imageFace = static::getImages($logo);
        $imageQrCode = static::getImages($qrCodePathPath);
        $qrCodeAndFacePathContent = $imageUtilModel->mergeFaceAndQrCode($imageQrCode,$imageFace,10,10,10,10);
        $qrCodeAndFacePathPath = $aliyunOSSModel -> put('qrgroupcodeface',$qrCodeAndFacePathContent);
        return $qrCodeAndFacePathPath ?: '';
    }





}
